<template>
    <div class="li">
        <div class="cont" @click="openChatInfo">
            <pub-image class="img" :src="getChatImg"></pub-image>
            <div class="text">
                <div class="title" v-if="chatInfo.type=='C2C'">
                    <div class="span_1" v-if="userRole==1"></div>
                    <div class="span_2" :class="userRole==1?'span_2_red':''"> {{getChatTitle }}</div>
                    <!-- <div class="span_3" v-if="userLevel>0">Lv.{{ userLevel }}</div> -->
                </div>
                <div class="title" v-if="chatInfo.type=='GROUP'">{{ getChatTitle }}</div>
                <div class="time" v-if="chatInfo.lastMessage">{{
                    $comFun.getTimeTxt('chat_time_deal',chatInfo.lastMessage.lastTime)
                    }}</div>
                <rich-text class="msg" :nodes="getLastMessageTxt"></rich-text>
                <!-- <div class="msg" v-html="chatInfo.lastMessageTxt"></div> -->
                <div class="num" :class="chatInfo.messageRemindType =='AcceptNotNotify'?'num_no':''"
                    v-if="chatInfo.unreadCount>0">
                    {{ chatInfo.unreadCount }}</div>
            </div>
        </div>
    </div>
</template>

<script>
import { replaceEmote } from '@/pages/chat/emote/emote.js';
export default {
    props: {
        chatInfo: {
            type: Object,
            default: () => ({})
        },
        friendInfoData: {
            type: Object,
            default: () => ({})
        },
    },
    data() {
        return {
            friendInfo: {}
        };
    },
    watch: {
        friendInfoData: {
            handler() {
                this.friendInfo = JSON.parse(JSON.stringify(this.friendInfoData));
            },
            immediate: true,
            deep: true
        }
    },
    computed: {
        userRole() {
            if (this.friendInfo[this.chatInfo.userProfile.userID]) {
                return this.friendInfo[this.chatInfo.userProfile.userID].role;
            } else {
                return 0;
            }
        },
        userLevel() {
            if (this.friendInfo[this.chatInfo.userProfile.userID]) {
                return this.friendInfo[this.chatInfo.userProfile.userID].level;
            } else {
                return 0;
            }
        },
        getChatTitle() {
            let info = this.chatInfo;
            if (info.type == 'C2C') {
                return info.remark ? info.remark : info.userProfile.nick
            } else if (info.type == 'GROUP') {
                return info.groupProfile.name
            }
        },
        getChatImg() {
            let info = this.chatInfo;
            if (info.type == 'C2C') {
                return info.userProfile.avatar
            } else if (info.type == 'GROUP') {
                return info.groupProfile.avatar
            }
        },
        getLastMessageTxt() {
            try {
                let info = this.chatInfo;
                let lastMessageTxt = ' ';
                if (info.lastMessage?.type == 'TIMTextElem') {
                    lastMessageTxt = replaceEmote(info.lastMessage.payload.text);
                }
                if (info.lastMessage?.type == 'TIMImageElem') {
                    lastMessageTxt = '[图片]';
                }
                if (info.lastMessage?.type == "TIMVideoFileElem") {
                    lastMessageTxt = '[视频]';
                }
                if (info.lastMessage?.type == 'TIMCustomElem') {
                    lastMessageTxt = this.fhandleCallKitSignaling(info.lastMessage);
                }
                if (info.lastMessage?.type == 'TIMSoundElem') {
                    lastMessageTxt = '[语音]';
                }

                // if (info.lastMessage?.customElem) {
                //     // lastMessageTxt = info.lastMessage.customElem.customMsg
                // }
                if (info.lastMessage?.type == "TIMGroupTipElem") {
                    // 处理群tips消息
                    let operationType = info.lastMessage.payload.operationType;
                    // 群tips消息(type:1有成员加群  2有群成员退群  3有群成员被踢出群  4有群成员被设为管理员
                    // 5有群成员被撤销管理员   6群组资料变更   7群成员资料变更，例如：群成员被禁言)
                    let tips = ['', '加入群聊', '退出群聊', '被踢出群聊', '', '', '修改了群信息', ''];
                    if ([1, 2, 3].indexOf(operationType) != -1) {
                        let memberList = info.lastMessage.payload.memberList ? info.lastMessage.payload.memberList.map(li => {
                            return li.nick;
                        }) : [];
                        lastMessageTxt = `${memberList.join("、")}${tips[operationType]}`
                    }
                    if ([6].indexOf(operationType) != -1) {
                        lastMessageTxt = `${info.lastMessage?.nick}${tips[operationType]}`
                    }
                }
                if (info.lastMessage?.isRevoked) {
                    lastMessageTxt = '消息已被管理员撤回';
                }
                return lastMessageTxt;
            } catch (error) {
                return '';
            }
        }
    },
    onLoad() { },
    methods: {
        openChatInfo() {
            this.$emit("openChatInfo")
        },
        fhandleCallKitSignaling(message) {
            try {
                const callMessage = JSON.parse(message.payload.data);
                if (callMessage?.businessID !== 1) {
                    return '';
                }
                const objectData = JSON.parse(callMessage?.data);
                let inviteeList = '';
                callMessage?.inviteeList?.forEach((userID) => {
                    inviteeList += `${userID}、`;
                });
                inviteeList = inviteeList.substring(0, inviteeList.lastIndexOf('、'));
                const userID = message.fromAccount || message.from; // 取 message.fromAccount 兼容会话 lastMsg
                let messageSender = message.nameCard || message.nick || userID;
                messageSender = messageSender.length > 12 ? `${messageSender.slice(0, 12)}...` : messageSender;
                switch (callMessage?.actionType) {
                    case 1: {
                        if (objectData?.data?.cmd === 'audioCall' || objectData?.data?.cmd === 'videoCall') {
                            if (callMessage?.groupID) {
                                return `${messageSender} 发起通话`;
                            }
                        }
                        if (objectData?.data?.cmd === 'hangup') {
                            if (callMessage?.groupID) {
                                return '通话结束';
                            }
                            return `通话时长：${this.$comFun.getTimeDiffTxt(objectData?.call_end)}`;
                        }
                        if (objectData?.data?.cmd === 'switchToAudio') {
                            return '切换语音通话';
                        }
                        if (objectData?.data?.cmd === 'switchToVideo') {
                            return '切换视频通话';
                        }
                        // CDM 异常时【发起通话】作为默认返回值
                        return '发起通话';
                    }
                    case 2:
                        if (callMessage?.groupID) {
                            return `${messageSender} 取消通话`;
                        }
                        return '取消通话';
                    case 3:
                        if (objectData?.data?.cmd === 'switchToAudio') {
                            return '切换语音通话';
                        }
                        if (objectData?.data?.cmd === 'switchToVideo') {
                            return '切换视频通话';
                        }
                        if (callMessage?.groupID) {
                            return `${messageSender} 已接听`;
                        }
                        return '已接听';
                    case 4:
                        if (callMessage?.groupID) {
                            return `${messageSender} 拒绝通话`;
                        }
                        return '拒绝通话';
                    case 5:
                        if (objectData?.data?.cmd === 'switchToAudio') {
                            return '切换语音通话';
                        }
                        if (objectData?.data?.cmd === 'switchToVideo') {
                            return '切换视频通话';
                        }
                        if (callMessage?.groupID) {
                            return `${inviteeList} 无应答`;
                        }
                        return '无应答';
                    default:
                        return '';
                }
            } catch (error) {
                console.error(error);
                return '';
            }
        },
    },
};
</script>

<style lang="scss" scoped>
@import "@/common/css/chat.scss";
</style>